package com.mxwl.cbg.common.config;

import com.fasterxml.jackson.databind.json.JsonMapper;
import lombok.extern.slf4j.Slf4j;

import java.io.IOException;
import java.io.InputStream;
import java.util.*;

@Slf4j
public class GameConfig {
    private static final Object lock = new Object();
    private static boolean isLoaded = false;
    private static Map<String, Object> game_config = new HashMap<>();

    private static void loadConfig() {
        synchronized (lock) {
            if (!isLoaded) {
                try (InputStream inputStream = GameConfig.class.getClassLoader().getResourceAsStream("game_config.json")) {
                    if (inputStream == null) {
                        throw new RuntimeException("Unable to find game_config.json in classpath.");
                    }
                    JsonMapper jsonMapper = new JsonMapper();
                    game_config = jsonMapper.readValue(inputStream, Map.class);
                    isLoaded = true;
                } catch (IOException e) {
                    throw new RuntimeException("Failed to load configuration", e);
                }
            }

        }
    }

    public static void main(String[] args) {

    }

    /**
     * 获取影响门派技能的法宝
     *
     * @return
     */
    public static ArrayList<Integer> getYxsmjnFabao() {
        Map<Integer, Map<String, Object>> fabaoInfo = getFabaoInfo();
        ArrayList<Integer> fb_ids = new ArrayList<>();
        for (Integer id : fabaoInfo.keySet()) {
            Map<String, Object> fabao = fabaoInfo.get(id);
            if ((Boolean) fabao.get("xympjn") == true) {
                fb_ids.add(id);
            }
        }
        return fb_ids;
    }


    /**
     * 根据宝石名称获取宝石id
     *
     * @param name
     * @return
     */
    public static Optional<Integer> getGemstoneId(String name) {
        Map<String, String> gemsName = (Map<String, String>) getGame_config().get("gems_name");
        for (String key : gemsName.keySet()) {
            if (gemsName.get(key).equals(name)) {
                return Optional.of(Integer.valueOf(key));
            }
        }
        return Optional.empty();
    }

    /**
     * 获取法宝的定义描述
     *
     * @return
     */
    public static Map<Integer, Map<String, Object>> getFabaoInfo() {
        Map<String, Object> map = getGame_config();
        return (Map<Integer, Map<String, Object>>) map.get("school_skill_desc");

    }

    /**
     * 获取所有临时符技能id
     *
     * @return
     */
    public static List<Integer> getLSFSkills() {
        Map<String, Map<String, String>> schoolSkillDesc = getSchoolSkillDesc();
        ArrayList<Integer> lsfSkillsList = new ArrayList<>();
        for (String key : schoolSkillDesc.keySet()) {
            String desc = schoolSkillDesc.get(key).get("desc");
            if (desc.contains("（临时符技能）")) {
                lsfSkillsList.add(Integer.parseInt(key));
            }

        }
        return lsfSkillsList;
    }

    /**
     * 根据技能id判断是否是师门技能
     *
     * @param skill_id
     * @return
     */
    public static boolean isSchoolSkill(String skill_id) {
        Map<String, Map<String, String>> schoolSkill = (Map<String, Map<String, String>>) getGame_config().get("school_skill");
        return schoolSkill.containsKey(skill_id);
    }

    /**
     * 获取师门技能描述
     *
     * @return
     */
    public static Map<String, Map<String, String>> getSchoolSkillDesc() {
        Map<String, Object> map = getGame_config();
        return (Map<String, Map<String, String>>) map.get("school_skill_desc");
    }

    /**
     * 获取剧情技能对照表
     *
     * @return
     */
    public static Map<String, String> getJuQingSkill() {
        Map<String, Object> map = getGame_config();
        return (Map<String, String>) map.get("ju_qing_skill");
    }

    /**
     * 获取生活技能对照表
     *
     * @return
     */
    public static Map<String, String> getLifeSkill() {
        Map<String, Object> map = getGame_config();
        return (Map<String, String>) map.get("life_skill");
    }

    /**
     * 获取所有部门技能对照表
     *
     * @return
     */
    public static Map<String, String> getSchoolSkill() {
        Map<String, Map<String, Object>> map = (Map<String, Map<String, Object>>) getGame_config().get("school_skill");
        HashMap<String, String> result = new HashMap<>();
        for (String id : map.keySet()) {
            result.put(id, (String) map.get(id).get("name"));
        }
        return result;
    }

    public static Map<String, Object> getGame_config() {
        if (!isLoaded) {
            loadConfig();
        }
        return game_config;
    }

    public static Object get(String key) {
        if (!isLoaded) {
            loadConfig();
        }
        return game_config.get(key);
    }

    /**
     * 获取所有的装备特效
     *
     * @return
     */
    public static Optional<Map<String, String>> getAllEquipmentEffect() {
        try {
            Map<String, Object> gameConfig = getGame_config();
            Map<String, String> texiaoInfo = (Map<String, String>) gameConfig.get("equip_special_effect");
            return Optional.of(texiaoInfo);
        } catch (NullPointerException e) {
            log.error("获取所有装备效果异常,请检查json配置文件", e);
        } catch (ClassCastException e) {
            log.error("装备效果数据转换,请检查json配置文件", e);
        }
        return Optional.empty();
    }


    public static Map<Integer, String> getWjd_equip_kindid() {
        List<Map<String, Object>> wjdEquipKindid = (List<Map<String, Object>>) getGame_config().get("wjd_equip_kindid");
        Map<Integer, String> result = new HashMap<>();
        for (Map<String, Object> data : wjdEquipKindid) {
            Object valueObj = data.get("value");
            String label = (String) data.get("label");

            int value;
            if (valueObj instanceof Integer) {
                value = (Integer) valueObj;
            } else if (valueObj instanceof String) {
                value = Integer.parseInt((String) valueObj);
            } else {
                throw new ClassCastException("Unsupported type for value: " + valueObj.getClass().getName());
            }

            result.put(value, label);
        }
        return result;
    }

    /**
     * 获取特技ID通过特技名称
     *
     * @param teji_name 特技名称
     * @return 返回一个Optional，包含如果找到则为特技的ID，否则为空
     */
    public static Optional<String> getTeJiIDByTeJiName(String teji_name) {
        if (teji_name.isBlank()){
            return Optional.empty();
        }
        List<List<Object>> datas = (List<List<Object>>) getGame_config().get("equip_special_skills");
        for (List<Object> data : datas) {
            try {
                String id = String.valueOf(data.get(0));
                String name = String.valueOf(data.get(1));
                if (name.equals(teji_name)) {
                    return Optional.of(id);
                }
            }catch (ClassCastException e){
                log.error("特技ID转换异常,请检查json配置文件", e);
            }
        }
        log.error("没有找到特技：{}", teji_name);
        return Optional.empty();
    }

    /**
     * 根据特效名称获取对应的ID。
     *
     * @param name 特效的名称
     * @return 返回一个Optional，如果找到则包含特技的ID，否则为空
     */
    public static Optional<Integer> getTeXiaoByName(String name) {
        if (name.isBlank()){
            return Optional.empty();
        }
        Map<String, String> map = (Map<String, String>) getGame_config().get("equip_special_effect");
        for (String id : map.keySet()) {
            if (map.get(id).equals(name)) {
                try {
                    return Optional.of(Integer.valueOf(id));

                }catch (ClassCastException e){
                    log.error("特效ID转换异常,请检查json配置文件", e);
                }
            }
        }
        log.error("没有找到特效：{}", name);
        return Optional.empty();
    }

    /**
     * 根据套装名称获取对应的ID。
     *
     * @param name 套装的名称
     * @return 返回一个Optional，如果找到则包含套装的ID，否则为空
     */
    public static Optional<Integer> getTaoZhuangByName(String name) {
        Map<String, String> map = (Map<String, String>) getGame_config().get("tao_zhuang");
        for (String id : map.keySet()) {
            if (map.get(id).equals(name)) {
                return Optional.of(Integer.valueOf(id));
            }
        }
        return Optional.empty();
    }

}
